<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/home/config.php';

$array_return = array();
$array_return['status'] = 0;
$array_return['html'] = '';

$page = getValue('page','int','POST',2);
$start = ($page - 1)*20;
$query = getValue('query','str','POST','');
$query = json_decode(base64_decode($query),1);
//$query = $query[0];
//$array_return['html'] = $query;
//die(json_encode($array_return));

$list_products = db_array('SELECT *
                           FROM accessories
                           LEFT JOIN categories ON acc_cat_id = cat_id
                           WHERE acc_active = 1'.$query.'
                           LIMIT '.$start.',20');
$total_products = count($list_products);
$list_instock_status = array(0=>'Hết hàng',1=>'Còn hàng',2=>'Sắp về',3=>'Sắp ra mắt');
if($total_products == 0) {
   $array_return['html'] = 'Hết sản phẩm';
}elseif($total_products > 0 && $total_products < 20) {
   foreach($list_products as $product) {
      $array_return['status'] = 2;
      $array_return['html'] .= '<div class="proitem">
                              <a href="'.url_accessories_detail($product).'">
                                 <img src="'.img_link($product['acc_image'],'small').'" alt="'.$product['acc_name'].'" />
                                 <p class="goods">'.$list_instock_status[$product['acc_instock_status']].'</p>
                                 <h2 class="name">'.$product['acc_name'].'</h2>
                                 <h4 class="price">'.format_price($product['acc_price']).'</h4>
                              </a>
                           </div>';
   }
}else {
   foreach($list_products as $product) {
      $array_return['status'] = 1;
      $array_return['html'] .= '<div class="proitem">
                              <a href="'.url_accessories_detail($product).'">
                                 <img src="'.img_link($product['acc_image'],'small').'" alt="'.$product['acc_name'].'" />
                                 <p class="goods">'.$list_instock_status[$product['acc_instock_status']].'</p>
                                 <h2 class="name">'.$product['acc_name'].'</h2>
                                 <h4 class="price">'.format_price($product['acc_price']).'</h4>
                              </a>
                           </div>';
   }
}

die(json_encode($array_return));
?>